package com.songmao.pricecloudapitest.testcase.user;

import com.songmao.pricecloudapitest.requestcore.ResponePoJo;
import com.songmao.pricecloudapitest.server.PriceCloudHttpCore;
import com.songmao.pricecloudapitest.utils.AssertCore;
import com.songmao.pricecloudapitest.utils.ParsingJson;
import com.songmao.pricecloudapitest.utils.TestData;
import com.songmao.pricecloudapitest.utils.TestUitl;
import io.qameta.allure.Description;
import io.qameta.allure.Step;
import org.testng.annotations.Test;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author Remy
 * @description :
 * @date 2020-05-08 16:01
 */
public class PriceCloudUser extends PriceCloudHttpCore {
//
    @Description("用户列表")
    @Step("用户列表,查询已启动标记的用户")
    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "用户列表,查询状态是0或者1的用户")
    public void selectEffectiveMarkUser(Map<String, String> map) {
        ResponePoJo rs = super.get(map.get("json"));
        //先判断是否请求成功
        AssertCore.assertEquals(map.get("expected"), rs.getCode());

        List<Object> enabledList = ParsingJson.getPathCodes(rs.getData().toString(), map.get("assertpath"));
        assert enabledList != null;
        for (Object i : enabledList) {
            AssertCore.assertEquals(map.get("twoexpected"), i.toString());
        }
    }


    @Description("用户列表")
    @Step("用户列表,查询全部用户")
    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "用户列表,查询全部用户")
    public void selectAllUser(Map<String, String> map) {
        ResponePoJo rs = super.get(map.get("json"));
        //先判断是否请求成功
        AssertCore.assertEquals(map.get("expected"), rs.getCode());

        List<Object> enabledList = ParsingJson.getPathCodes(rs.getData().toString(), map.get("assertpath"));
        assert enabledList != null;
        AssertCore.assertTrue(enabledList.size() <= Integer.parseInt(map.get("twoexpected")), "返回的用户列表大于用户设置");

    }

    @Description("用户列表")
    @Step("用户列表,查询某个用户")
    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "用户列表,查询某个用户")
    public void selectFindUser(Map<String, String> map) {
        ResponePoJo rs = super.get(map.get("json"));
        //先判断是否请求成功
        AssertCore.assertEquals(map.get("expected"), rs.getCode());
        String json = rs.getData().toString();
        AssertCore.assertTrue(json != null, "返回结果异常,请检查返回结果");
        AssertCore.assertEquals(map.get("twoexpected"),
                Objects.requireNonNull(ParsingJson.getPathCode(json, map.get("assertpath"))).toString());
    }


    @Description("用户列表")
    @Step("用户列表,查询某个用户")
    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "用户列表,查询某个用户")
    public void selectFindTimeUser(Map<String, String> map) {
        ResponePoJo rs = super.get(map.get("json"));
        //先判断是否请求成功
        AssertCore.assertEquals(map.get("expected"), rs.getCode());
        String json = rs.getData().toString();
        AssertCore.assertTrue(json != null, "返回结果异常,请检查返回结果");

        //获取预期的筛选条件; 开始时间，结束时间
        Map<String, String> expectedMap = TestUitl.splitString(map.get("twoexpected"));
        assert expectedMap != null;

        Date startTime = TestUitl.stringConvertDate(expectedMap.get("startTime"), null);
        Date endTime = TestUitl.stringConvertDate(expectedMap.get("endTime"), null);

        //获取json内返回用户的全部创建时间
        List<Object> actualList = ParsingJson.getPathCodes(json, map.get("assertpath"));
        assert actualList != null;
        for (Object obj : actualList) {
            //从日期格式转换成date
            Date intervalTime = TestUitl.stringConvertDate(obj.toString(), null);
            //开始判断返回的日期格式是否在开始时间和创建时间内;
            boolean actual = TestUitl.judgingTimePeriod(startTime, endTime, intervalTime);
            //进行断言
            AssertCore.assertEquals(Boolean.toString(actual), expectedMap.get("expected"));
        }
    }
}